package com.zlx.exchange.user.service.impl;


import cn.hutool.core.util.RandomUtil;
import cn.hutool.crypto.SecureUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zlx.exchange.domain.entity.User;
import com.zlx.exchange.user.dao.UserDao;
import com.zlx.exchange.user.service.UserService;
import org.springframework.stereotype.Service;
import com.github.pagehelper.PageInfo;

import javax.annotation.Resource;

import org.springframework.http.ResponseEntity;
import com.github.pagehelper.PageHelper;

import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.Random;

/**
 * (User)表服务实现类
 *
 * @author makejava
 * @since 2023-09-04 18:59:42
 */
@Transactional
@Service("userService")
public class UserServiceImpl implements UserService {
    @Resource
    private UserDao userDao;

    /**
     * 账户登录
     *
     * @param username
     * @param password
     * @return
     */
    @Override
    public User queryByUsername(String username, String password) {
        return userDao.selectOne(new LambdaQueryWrapper<User>()
                                     .eq(User::getUsername, username)
                                     .eq(User::getPassword, password));
    }

    /**
     * 用户注册
     */
    public Boolean register(String username, String password) {
        int insert = userDao.insert(new User(null, username, SecureUtil.md5(password), String.valueOf(RandomUtil.randomInt(1000, 10000)), new Date(), null, 1));
        return insert > 0;
    }

}
